<?php

/*
 * Bdata程序一部分：主要用于数据挖掘功能
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Controller;

/**
 * Description of BdataDigController
 *
 * @author chenghui
 */
class BdataDigController extends BdataFetchController {
    
    public function getDigResults(){
        
    }
    
    /**
     * @解析传入参数
     * @param type $post
     */
    private function setPostParam($post){
        foreach ($post as $key=>$val){
            if(is_array($var)){
                if(!empty($val)){
                    $this->param['where'][$key]=$val;
                }                
            }else{
                if(!empty($val)){
                    if(strpos($val,'@')){//存在多个维度值时
                        $k = explode(',', $key);
                        $v = explode('@', $val);
                        foreach ($k as $index=>$values){
                            $key=  strtoupper($values);
                            $this->setWhereString($key,$v[$index]);
                        }
                    }else{//单一值时
                        if($key!='type'){
                            $this->setWhereString($key,$val);
                        }                        
                    }
                }                
            }
        }
    }
    
    /**
     * 
     * @param type $key
     * @param type $val 字符串
     */
    private function setWhereString($key,$val){
        switch ($this->AxisConfig[$key]['FieldType']){
            case 1://对照
                $this->param['where'][$key]=$val;
                break;
            case 3://统计
                unset($this->param['where'][$key]);

                if(is_string($val)){//存在多个维度值时
                    $reg = explode('-', $val);
                    $this->param['where'][$key][]=$reg[0];
                    $this->param['where'][$key][]=$reg[1];
                }elseif(is_array($val)){
                    $this->param['where'][$key][]=$val[0];
                    $this->param['where'][$key][]=$val[1];
                }                
                break;
            case 2://日期
                $this->setDateValue($key,$val);//返回条件组合
                break;
            case 4://描述
                $this->param['where'][$key]=$val;
                break;
        }
    }


    private function setDateValue($key,$val){
        //获取日期型字段的基础表字段名
        $DataColumn = $this->AxisConfig[$key]['DataColumn'];
        //
        unset($this->param['where'][$DataColumn]);
        
        switch ($this->AxisConfig[$key]['Expression']){
            case 'Y'://年度
                $this->param['where'][$key] = $val;
                break;
            case 'YM':
                //$year = mb_substr($val,0,4,'utf-8');
                $date = explode('-', $val);
                $this->param['where']['YY'] = $date[0];
                
                //$month = mb_substr($val,5,2,'utf-8');
                $this->param['where']['YM'] = $date[1];
                break;
            case 'YW':
                $date = explode('-', $val);
                $this->param['where'][$DataColumn][] = $date[0];
                $this->param['where'][$DataColumn][] = $date[1];
                break;
            case 'YQ':
                //$year = mb_substr($val,0,4,'utf-8');
                $date = explode('-', $val);
                $this->param['where']['YY'] = $date[0];
                
                //$q = mb_substr($val,6,2,'utf-8');
                $this->param['where']['YQ'] = $date[1];
                break;
            case 'YB':
                $date = explode('-', $val);
                $year = $date[0];//mb_substr($val,0,4,'utf-8');
                
                $left = $date[1];//mb_substr($val,5,3,'utf-8');
                
                if($left==1){
                    $this->param['where'][$DataColumn][] = $year.'-01-01';
                    $this->param['where'][$DataColumn][] = $year.'-06-30';
                }else{
                    $this->param['where'][$DataColumn][] = $year.'-07-01';
                    $this->param['where'][$DataColumn][] = $year.'-12-31';
                }
                break;
            case 'D':
            default :
                $this->param['where'][$DataColumn] = $val;
                break;
        }
    }
}
